package com.frogs.ucenter.controller;

import java.util.Arrays;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.EvaluateService;

/**
 * 验证检查类
 * @author wushubin
 *
 */
@Controller
public class EvaluateController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(EvaluateController.class);

	private static final String HOME_VIEW = "evaluate/index";

	private static final String HOME = "/evaluate/index";
	private static final String LIST = "/evaluate/list";
	private static final String DELETE = "/evaluate/delete";
	private static final String UPDATE = "/evaluate/update";
	private static final String REDIRECT_HOME = "redirect:/evaluate/index.html";
    
    @Resource(name="evaluateService")
    private EvaluateService evaluateService;
    
	@Resource
	private UserPermissionHelper userPermissionHelper;
    
    @RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 20);
    		
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<Map<String,Object>> object = evaluateService.getEvaluateList(pageNo, pageSize, user.getId());
    		model.addAttribute("list",object );
    		model.addAttribute("pageTurn",object.getPageTurn() );
    		
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }

	@RequestMapping(DELETE)
	public JSONObject delete(HttpServletRequest request){
		try {
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			
			int evaluationId = reqInfo.getIntParameter("evaluationId", -1);
			if(evaluationId <= 0) {
				throw new Exception("非法的参数!");
			}
			int goodsId = reqInfo.getIntParameter("goodsId", -1);
			if(goodsId <= 0) {
				throw new Exception("非法的参数!");
			}
			return evaluateService.deleteEvaluate(user.getId(),goodsId, evaluationId);
		} catch (Exception e) {
			log.error("func[delete] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });

		}
		return null;
	}
	
	@RequestMapping(UPDATE)
	public String update(HttpServletRequest request){
		try {
			HttpRequestInfo reqInfo = new HttpRequestInfo(request);
			int goodsId = reqInfo.getIntParameter("goodsId");
			String content = reqInfo.getParameter("content");
			
    		
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		evaluateService.updateEvaluate(user.getId(), goodsId, content);
    		
		} catch (Exception e) {
			log.error("func[update] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
		return REDIRECT_HOME;
	}
	
	@RequestMapping(LIST)
    public void list(HttpServletRequest request,ModelMap model) throws Exception {
		try{
			
			
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
	    	
			//商品评价
	    	model.put("list", evaluateService.getEvaluateList(1, 3, user.getId()));
		} catch (Exception e) {
			log.error("func[list] parameterMap[{}] exception[{} - {}] desc[fail]",
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
	}
}
